﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHXH.Core.IService;
using FX.Core;
using CRM.Admin;
using BHXH.Core.Domain;
using NHibernate.Cfg;
using NHibernate;
using System.Text.RegularExpressions;


namespace BHXH.Frm.Frm
{
    public partial class ThongTinChung : UserControl
    {
        private readonly IdonvibhxhService _DonvibhxhSrv;
        private readonly IthongtincongtyService _thongtincongtySrv;
        private readonly IthanhphoService _thanhphoSrv;
        private readonly IquanhuyenService _quanhuyenSrv;
        string KinhDoanhKhuKinhTe = "";
        static string Masothue = "01696223732";
        public ThongTinChung()
        {
            InitializeComponent();
            _DonvibhxhSrv = IoC.Resolve<IdonvibhxhService>();
            _thongtincongtySrv = IoC.Resolve<IthongtincongtyService>();
            _thanhphoSrv = IoC.Resolve<IthanhphoService>();
            _quanhuyenSrv = IoC.Resolve<IquanhuyenService>();
            Load_cbbNamTaiChinh();
            Load_cbbThanhPho();
            Load_cbbLoaiHinhKD();

            GetDonviByID(Masothue);

        }        

        #region "ThongTinChung"
        public void Load_cbbQuanHuyen(int MaTP)
        {
            var quanhuyen = _quanhuyenSrv.GetQuanHuyenByID(MaTP);          
            cbbMaQuanHuyen.DataSource = quanhuyen;
            cbbMaQuanHuyen.DisplayMember = "DistName";
            cbbMaQuanHuyen.ValueMember = "ID";
        }
        private void cbbTinhThanhPho_SelectedIndexChanged(object sender, EventArgs e)
        {

            string Matp = ((BHXH.Core.Domain.thanhpho)(cbbTinhThanhPho.SelectedItem)).ID.ToString();

            if (Matp != "0")
            {
                Load_cbbQuanHuyen(int.Parse(Matp));
            }

        }       

        public void Load_cbbThanhPho()
        {
            var ListThanhPho = _thanhphoSrv.layTatCaThanhPho();
            cbbTinhThanhPho.DataSource = ListThanhPho;
            cbbTinhThanhPho.DisplayMember = "Ten";
            cbbTinhThanhPho.ValueMember = "ID";
        }
        public void GetDonviByID(string MaSoThue)
        {

            var ThongtinDV = _thongtincongtySrv.GetDonViByID(MaSoThue);
            if (ThongtinDV.ID != 0)
            {
                lblID.Text = ThongtinDV.ID.ToString();
                txtTenDV.Text = ThongtinDV.TenCongTy;
                txtMaSoThue.Text = ThongtinDV.MaSoThue;
                cbbNamTaiChinh.SelectedValue = int.Parse(ThongtinDV.NamTaiChinh);
                txtDiaChi.Text = ThongtinDV.DiaChi;
                cbbTinhThanhPho.SelectedValue = ThongtinDV.thanhpho.ID;
                cbbMaQuanHuyen.SelectedValue = int.Parse(ThongtinDV.MaQuanHuyen);
                txtDiDong.Text = ThongtinDV.DiDong;
                txtDienThoai.Text = ThongtinDV.DienThoai;
                txtFax.Text = ThongtinDV.Fax;
                txtSoTaiKhoan.Text = ThongtinDV.SoTaiKhoan;
                txtNoiMoTaiKhoan.Text = ThongtinDV.NoiMoTaiKhoan;
                txtNguoiKyToKhai.Text = ThongtinDV.NguoiKyToKhai;
                txtChucDanh.Text = ThongtinDV.ChucDanh;
                cbbLoaiHinhKinhDoanh.SelectedValue = int.Parse(ThongtinDV.MaLoaiHinhKD);
                if (ThongtinDV.KinhDoanhKhuKinhTe == "1")
                {
                    ckKinhDoanhKhuKinhTe.Checked = true;
                }
                else
                {
                    ckKinhDoanhKhuKinhTe.Checked = false;
                }
                SetEnable(false);
                //BtnSua.Enabled = true;
            }
            else
            {
                lblID.Text = "0";
                SetEnable(true);
                //BtnSua.Enabled = false;
            }
        }
        public void Load_cbbNamTaiChinh()
        {
            cbbNamTaiChinh.DataSource = EnumHelper.GetDescriptionForBind(Ttdv_NamTaiChinh.N0101).ToList();
            cbbNamTaiChinh.DisplayMember = "ValueString";
            cbbNamTaiChinh.ValueMember = "ValueInt";
        }
       
        public void Load_cbbLoaiHinhKD()
        {
            cbbLoaiHinhKinhDoanh.DataSource = EnumHelper.GetDescriptionForBind(Ttdv_LoaiHinhKD.KD1).ToList();
            cbbLoaiHinhKinhDoanh.DisplayMember = "ValueString";
            cbbLoaiHinhKinhDoanh.ValueMember = "ValueInt";
        }
        public bool GetForm()
        {
            if (txtTenDV.Text == "" && txtMaSoThue.Text == ""
                && txtDiaChi.Text == "" && cbbMaQuanHuyen.SelectedIndex == -1 && cbbTinhThanhPho.SelectedIndex == -1
                && cbbTenPhuongXa.SelectedIndex == -1 && txtDiDong.Text == "" && txtDienThoai.Text == ""
                && txtFax.Text == "" && txtSoTaiKhoan.Text == "" && txtNoiMoTaiKhoan.Text == "" && txtNguoiKyToKhai.Text == ""
                && txtChucDanh.Text == "" && cbbLoaiHinhKinhDoanh.SelectedIndex == -1 && ckKinhDoanhKhuKinhTe.Checked == false)
            {
                return true;
            }
            else return false;

        }
        public void SetEnable(bool b)
        {
            txtTenDV.Enabled = b;
            txtMaSoThue.Enabled = b;
            cbbNamTaiChinh.Enabled = b;
            txtDiaChi.Enabled = b;
            cbbTinhThanhPho.Enabled = b;
            cbbMaQuanHuyen.Enabled = b;
            cbbTenPhuongXa.Enabled = b;
            txtDiDong.Enabled = b;
            txtDienThoai.Enabled = b;
            txtFax.Enabled = b;
            txtSoTaiKhoan.Enabled = b;
            txtNoiMoTaiKhoan.Enabled = b;
            txtNguoiKyToKhai.Enabled = b;
            txtChucDanh.Enabled = b;
            cbbLoaiHinhKinhDoanh.Enabled = b;
            ckKinhDoanhKhuKinhTe.Enabled = b;            
        }
        private void BtnSua_Click(object sender, EventArgs e)
        {
            //suaclick();
        }
        public void suaclick()
        {
            SetEnable(true);            
        }
        public void Them_Sua_ThongTin()
        {
            thongtincongty ttct = new thongtincongty();
            if (lblID.Text == "0")
            {
                ttct.ID = int.Parse(lblID.Text);
                ttct.TenCongTy = txtTenDV.Text;
                ttct.MaSoThue = txtMaSoThue.Text;
                ttct.NamTaiChinh = cbbNamTaiChinh.SelectedValue.ToString();
                ttct.thanhpho = _thanhphoSrv.Getbykey(int.Parse(cbbTinhThanhPho.SelectedValue.ToString()));
                ttct.MaQuanHuyen = cbbMaQuanHuyen.SelectedValue.ToString();                
                ttct.DiaChi = txtDiaChi.Text;
                ttct.DiDong = txtDiDong.Text;
                ttct.DienThoai = txtDienThoai.Text;
                ttct.Fax = txtFax.Text;
                ttct.SoTaiKhoan = txtSoTaiKhoan.Text;
                ttct.NoiMoTaiKhoan = txtNoiMoTaiKhoan.Text;
                ttct.NguoiKyToKhai = txtNguoiKyToKhai.Text;
                ttct.ChucDanh = txtChucDanh.Text;
                ttct.MaLoaiHinhKD = cbbLoaiHinhKinhDoanh.SelectedValue.ToString();
                if (ckKinhDoanhKhuKinhTe.Checked == true)
                {
                    ttct.KinhDoanhKhuKinhTe = "1";
                }
                else
                {
                    ttct.KinhDoanhKhuKinhTe = "0";
                }
                if (_thongtincongtySrv.AddDonVi(ttct) == true)
                {
                    MessageBox.Show(MainResource.msgLuuThanhCong);
                }
            }
            else
            {
                ttct.ID = int.Parse(lblID.Text);
                ttct.TenCongTy = txtTenDV.Text;
                ttct.MaSoThue = txtMaSoThue.Text;
                ttct.NamTaiChinh = cbbNamTaiChinh.SelectedValue.ToString();
                ttct.thanhpho = _thanhphoSrv.Getbykey(int.Parse(cbbTinhThanhPho.SelectedValue.ToString()));
                ttct.MaQuanHuyen = cbbMaQuanHuyen.SelectedValue.ToString();                
                ttct.DiaChi = txtDiaChi.Text;
                ttct.DiDong = txtDiDong.Text;
                ttct.DienThoai = txtDienThoai.Text;
                ttct.Fax = txtFax.Text;
                ttct.SoTaiKhoan = txtSoTaiKhoan.Text;
                ttct.NoiMoTaiKhoan = txtNoiMoTaiKhoan.Text;
                ttct.NguoiKyToKhai = txtNguoiKyToKhai.Text;
                ttct.ChucDanh = txtChucDanh.Text;
                ttct.MaLoaiHinhKD = cbbLoaiHinhKinhDoanh.SelectedValue.ToString();
                if (ckKinhDoanhKhuKinhTe.Checked == true)
                {
                    ttct.KinhDoanhKhuKinhTe = "1";
                }
                else
                {
                    ttct.KinhDoanhKhuKinhTe = "0";
                }
                if (_thongtincongtySrv.UpdateDonVi(ttct) == true)
                {
                    MessageBox.Show(MainResource.msgCapNhatThanhCong);
                }
            }
            SetEnable(false);

        }        
        public void btnLuu_Click()
        {
            if (Check() == true)
            {
                Them_Sua_ThongTin();
            } 
        }
        public void Exit_App()
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn thoát không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Application.ExitThread();
            }
        } 
        public void btnBoqua_click()
        {
            SetEnable(false);
        }        
        public bool Check()
        {
            if (txtTenDV.Text == "")
            {
                MessageBox.Show(MainResource.msgThieuTenDV);
                txtTenDV.Focus();
                return false;
            }
            else if (txtMaSoThue.Text == "")
            {
                MessageBox.Show(MainResource.msgThieuMaSoThue);
                txtMaSoThue.Focus();
                return false;
            }
            else if (txtDiaChi.Text == "")
            {
                MessageBox.Show(MainResource.msgThieuDiaChi);
                txtDiaChi.Focus();
                return false;
            }
            else if (txtDiDong.Text == "")
            {
                MessageBox.Show(MainResource.msgThieuDiDong);
                txtDiDong.Focus();
                return false;
            }
            else if (txtDienThoai.Text == "")
            {
                MessageBox.Show(MainResource.msgThieuDienThoai);
                txtDienThoai.Focus();
                return false;
            }
            else if (txtNguoiKyToKhai.Text == "")
            {
                MessageBox.Show(MainResource.msgThieuNguoiKyToiKhai);
                txtNguoiKyToKhai.Focus();
                return false;
            }
            else return true;
        }
        #endregion

    }
}
